(function (window, $, undefined) {
  'use strict';

  var dictData = {
    status: {

    },
    cityId: {

    }
  };

  var levelData = {};

  main(function () {

    var $tableLevel = $('#table-level');
    util.jqGrid($tableLevel, {
      url: '/json/test.json',
      // datatype: 'json',
      autowidth: true,
      height: $(window).height() - 350,
      multiselect: true,
      multiboxonly: true,
      // shrinkToFit: false,
      colModel: [
        { name: 'kid', key: true, hidden: true },
        { label: 'ID', name: 'kid', width: 60 },
        {
          label: '状态', name: 'status', width: 40, formatter: function (v) {
            return dictData.status[v] || '';
          }
        },
        {
          label: '城市', name: 'cityId', width: 50, formatter: function (v) {
            return dictData.cityId[v] || '';
          }
        },
        { label: '等级', name: 'level', width: 60 },
        {
          label: '录入时间', name: 'createTime', width: 80, formatter: function (v, o, r) {
            return util.getDate(v);
          }
        }
      ],
      loadComplete: function (res) {
        levelData = {};
        if (res && res.data) {
          $.each(res.data, function (_, item) {
            levelData[item.kid] = item;
          });
        }
      },
      pager: '#pager-level'
    });

    // 获取城市列表
    util.ajax(ajaxURL.ORGANIZATION + '/city/getCityList?pageNum=1&pageSize=99999', '', 'GET', util.resCallback(function (res) {
      util.selectHtml($('[name="cityId"]'), res.data, 'cityId', 'name', '请选择');
      $.each(res.data, function (_, item) {
        dictData.cityId[item.cityId] = item.name;
      });
      searchTable();
    }));

    util.getDict('status', 'status').then(function (data) {
      util.selectHtml('[name="status"]', data, 'value', 'name', '请选择');
      $.each(data, function (_, item) {
        dictData.status[item.value] = item.name;
      });
    });

    function searchTable() {
      var data = util.formData('#form-search');
      util.searchGrid($tableLevel, data);
    }

    function searchTable() {
      var data = util.formData('#search-form');
      util.searchGrid($tableLevel);
    }

    laydate.render({
      elem: '#createTime' //指定元素
    });

    var $layerLevel = $('#layer-level-detail');
    var $formLevel = $('#form-level-detail');
    var $saveLevel = $('#level-save');

    // 新增操作
    $('#btn-add').click(function () {
      $layerLevel.find(':input').removeAttr('disabled');
      util.layerOpen({
        title: '新增人力等级',
        content: $layerLevel,
        area: ['500px', ''],
        cancel: function (index) {
          layer.confirm('确定退出当前页面？', {
            btn: ['确定', '再想一想'],
            yes: function (idx) {
              layer.close(index);
              layer.close(idx);
            }
          });
          return false;
        }
      });
    });

    // 修改操作
    $('#btn-edit').click(util.oneCheck($tableLevel, function (id) {

      $layerLevel.find(':input').removeAttr('disabled');

      var data = levelData[id];
      util.fillForm($formLevel, data);

      util.layerOpen({
        title: '修改人力等级',
        content: $layerLevel,
        area: ['500px', '']
      });
    }));

    $('#level-save').click(function () {

      // if(util.chechForm($formSupplier)) {
        var data = util.formData($formLevel);

        // data.kid 来决定是什么操作，有为修改，无为新增
        // var url = ajaxURL.ORGANIZATION + (data.kid ? '/auto/updateAutoType' : '/auto/addAutoType');
        // util.ajax(url, JSON.stringify(data), 'POST', util.resCallback(function (res) {
        //   layer.closeAll();
        //   util.tip.success('操作成功');
        //   $tableDriver.trigger('reloadGrid');
        // }));

      // }else {
      //   util.tip.warn('请将数据填写完整');
      // }

    });

  });


})(window, jQuery);
